﻿using System;

/// <summary>
///Pagecode 数字分页
/// </summary>
public static class Pagecode
{
    /// <summary>
    /// 数字分页
    /// </summary>
    /// <param name="ToatalCountRecord">总记录数</param>
    /// <param name="PageItem">每页显示数</param>
    /// <param name="CurrentPage">当前页数</param>
    /// <param name="IStep">偏移量 一般为5</param>
    /// <param name="ILeftNum">左界限 一般为0</param>
    /// <param name="IRightNum">右界限 一般为0</param>
    /// <param name="PageUrl">href内分页链接地址。如 Showlist</param>
    /// <param name="Fengefu">分页数字之前分隔符 如_</param>
    /// <param name="HouZui">href内分页地址后缀格式 如 html</param>
    /// <returns>返回静态分页html代码 </returns>
    public static string BuildPages(int ToatalCountRecord, int PageItem, int CurrentPage, int IStep, int ILeftNum, int IRightNum, string PageUrl, string Fengefu, string HouZui)
    {

        int Step = IStep;//偏移量 
        int LeftNum = ILeftNum;//左界限 
        int RightNum = IRightNum;//右界限 
        // string PageUrl = Request.FilePath;
        int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);//获取总页数
        if (CurrentPage - Step < 1)
        {
            LeftNum = 1;
        }
        else
        {
            LeftNum = CurrentPage - Step;
        }
        if (CurrentPage + Step > PageCount)
        {
            RightNum = PageCount;
        }
        else
        {
            RightNum = CurrentPage + Step;
        }
        string OutPut = "";
        if (CurrentPage > 1)
        {
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "1" + HouZui + "' title='第一页'>  &lt;&lt; </a>";//如果不是首页。跳到最后首页页为可用
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + (CurrentPage - 1) + "" + HouZui + "' title='向前一页'>  &lt; </a>";//如果不是第一页。则向前翻页状态为可用
        }
        else
        {
            OutPut += " <span class='disabled'>&lt;&lt; </span>";//如果是首页。跳到最后首页页不可用
            OutPut += " <span class='disabled'>&lt; </span>";//如果是第一页。则向前翻页状态不可用
        }
        for (int i = LeftNum; i <= RightNum; i++)
        {
            if (i == CurrentPage)
            {
                OutPut += "<span class='current'>" + i.ToString() + "</span>";//当前显示页为不可用状态
            }
            else
            {
                OutPut += " <a href='" + PageUrl + "" + Fengefu + "" + i.ToString() + "" + HouZui + "' title='第" + i.ToString() + "页'>&nbsp;" + i.ToString() + "&nbsp;</a>";//不是当前页的页面为可用。
            }
        }
        if (CurrentPage < PageCount)
        {

            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + (CurrentPage + 1) + "" + HouZui + "' title='向后一页'> &gt;</a>";//如果不是末页。则向后翻页状态为可用
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + PageCount + "" + HouZui + "' title='末页'> &gt;&gt; </a>";//如果不是末页。跳到最后一页为可用
        }
        else
        {

            OutPut += "<span class='disabled'> &gt; </span>";//如果是末页。则向后翻页状态不可用
            OutPut += "<span class='disabled'> &gt;&gt; </span>";//如果是末页。跳到最后一页不可用
        }
        if (ToatalCountRecord > PageItem)
        {
            return OutPut;
        }
        else
        {
            return "";
        }
    }
    public static string BuildPages2(int ToatalCountRecord, int PageItem, int CurrentPage, int IStep, int ILeftNum, int IRightNum, string PageUrl, string Fengefu, string HouZui)
    {

        int Step = IStep;//偏移量 
        int LeftNum = ILeftNum;//左界限 
        int RightNum = IRightNum;//右界限 
        // string PageUrl = Request.FilePath;
        int PageCount = (int)Math.Ceiling((double)(ToatalCountRecord) / PageItem);//获取总页数
        if (CurrentPage - Step < 1)
        {
            LeftNum = 1;
        }
        else
        {
            LeftNum = CurrentPage - Step;
        }
        if (CurrentPage + Step > PageCount)
        {
            RightNum = PageCount;
        }
        else
        {
            RightNum = CurrentPage + Step;
        }
        string OutPut = "";
        if (CurrentPage > 1)
        {
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "1." + HouZui + "' title='第一页'>  &lt;&lt; </a>";//如果不是首页。跳到最后首页页为可用
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + (CurrentPage - 1) + "." + HouZui + "' title='向前一页'>  &lt; </a>";//如果不是第一页。则向前翻页状态为可用
        }
        else
        {
            OutPut += " <span class='disabled'>&lt;&lt; </span>";//如果是首页。跳到最后首页页不可用
            OutPut += " <span class='disabled'>&lt; </span>";//如果是第一页。则向前翻页状态不可用
        }
        for (int i = LeftNum; i <= RightNum; i++)
        {
            if (i == CurrentPage)
            {
                OutPut += "<span class='current'>" + i.ToString() + "</span>";//当前显示页为不可用状态
            }
            else
            {
                OutPut += " <a href='" + PageUrl + "" + Fengefu + "" + i.ToString() + "." + HouZui + "' title='第" + i.ToString() + "页'>&nbsp;" + i.ToString() + "&nbsp;</a>";//不是当前页的页面为可用。
            }
        }
        if (CurrentPage < PageCount)
        {

            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + (CurrentPage + 1) + "." + HouZui + "' title='向后一页'> &gt;</a>";//如果不是末页。则向后翻页状态为可用
            OutPut += "<a href='" + PageUrl + "" + Fengefu + "" + PageCount + "." + HouZui + "' title='末页'> &gt;&gt; </a>";//如果不是末页。跳到最后一页为可用
        }
        else
        {

            OutPut += "<span class='disabled'> &gt; </span>";//如果是末页。则向后翻页状态不可用
            OutPut += "<span class='disabled'> &gt;&gt; </span>";//如果是末页。跳到最后一页不可用
        }
        if (ToatalCountRecord > PageItem)
        {
            return OutPut;
        }
        else
        {
            return "";
        }
    }
}
